package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.parse.SubSeqOfRetOMDData;

import java.io.IOException;
public class ActionResponseNormalList {
    private byte[] dataCode = null;
    private PIID piidAcd = null;
    private SubSeqOfRetOMDData results = null;

    public ActionResponseNormalList() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public PIID getPiidAcd() {
        return piidAcd;
    }

    public SubSeqOfRetOMDData getResults() {
        return results;
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piidAcd, SubSeqOfRetOMDData results) {
        this.piidAcd = piidAcd;
        this.results = results;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piidAcd = new PIID();
        this.piidAcd.setNumBit(8);
        codeLength += this.piidAcd.decode(input);
        this.results = new SubSeqOfRetOMDData();
        codeLength += this.results.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.results != null && this.piidAcd != null) {
                codeLength += this.results.encode(output);
                codeLength += this.piidAcd.encode(output);
            } else {
                throw new RuntimeException("piid_acd or results is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piidAcd != null && this.results != null) {
            return "sequence: {" + "piid_acd: " + this.piidAcd + ", results: " + this.results + "}";
        } else {
            return "piid_acd or results is null";
        }
    }
}
